package com.tedu.ds.base.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class LogControllerAspect {
    @Pointcut("execution(public * com.tedu.ds.*.controller.*.*(..))")
    public void logPointcut() {}

    @Before("logPointcut()")
    public void logBefore(JoinPoint joinPoint) {
        String className = joinPoint.getTarget().getClass().getSimpleName();
        String methodName = joinPoint.getSignature().getName();
        String args= Arrays.toString(joinPoint.getArgs());
        log.info(className + "." + methodName + "()方法开始执行，参数为： " + args);
    }
    @AfterReturning(value = "logPointcut()",returning = "returnValue")
    public void afterReturning(JoinPoint joinPoint,Object returnValue){
        String className=joinPoint.getTarget().getClass().getSimpleName();
        String methodName=joinPoint.getSignature().getName();
        log.info(className+","+methodName+"()方法执行成功，结果为："+returnValue);
    }
    @AfterThrowing(value = "logPointcut()",throwing = "e")
    public void afterThrowing(JoinPoint joinPoint,Exception e){
        String className=joinPoint.getTarget().getClass().getSimpleName();
        String methodName=joinPoint.getSignature().getName();
        log.error(className+","+methodName+"()方法抛出异常，异常信息为："+e.getMessage());
    }
    @After("logPointcut()")
    public void logAfter(JoinPoint joinPoint){
        String className=joinPoint.getTarget().getClass().getSimpleName();
        String methodName=joinPoint.getSignature().getName();
        log.info(className+","+methodName+"()方法执行结束了");
    }
}
